package cn.cs.service.impl;

import cn.cs.client.RestFaultTypeClient;
import cn.cs.client.RestShopClient;
import cn.cs.common.Constants;
import cn.cs.common.EmptyUtils;
import cn.cs.common.Page;
import cn.cs.pojo.FaultType;
import cn.cs.pojo.Shop;
import cn.cs.service.ShopService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

@Service
public class ShopServiceImpl implements ShopService {

    @Resource
    private RestShopClient restShopClient;

    @Resource
    private RestFaultTypeClient faultTypeClient;


    public List<Shop>	getShopList(Map<String,Object> param)throws Exception{
        List<Shop> shopList = restShopClient.getShopList(param);
        for(Shop s:shopList) {
            String fIds =s.getProduct();
            List<String> list = new ArrayList<>();
            for(String a:fIds.split(",")){
                list.add(a);
            }
            Map<String, Object> map = new HashMap<String, Object>();
            param.put("ids", list);
            param.put("parentId", null);
            List<FaultType> faultTypeList = faultTypeClient.getFaultTypeEntityByParentId(param);
            List<String> typeName = new ArrayList<>();
            for(FaultType a:faultTypeList){
                typeName.add(a.getFaultName());
            }
            s.setTypeName(typeName);
        }
        return shopList;
    }
    public Shop getShopById(Long id)throws Exception{
        return restShopClient.getShopById(id);
    }

    public List<Shop>	getShopListByMap(Map<String,Object> param)throws Exception{
        return restShopClient.getShopListByMap(param);
    }

    public Integer getShopCountByMap(Map<String,Object> param)throws Exception{
        return restShopClient.getShopCountByMap(param);
    }

    public Page<Shop> queryShopPageByMap(Map<String,Object> param, Integer pageNo, Integer pageSize)throws Exception{
        Integer total = restShopClient.getShopCountByMap(param);
        pageNo = EmptyUtils.isEmpty(pageNo) ? Constants.DEFAULT_PAGE_NO : pageNo;
        pageSize = EmptyUtils.isEmpty(pageSize) ? Constants.DEFAULT_PAGE_SIZE : pageSize;
        Page page = new Page(pageNo, pageSize, total);
        param.put("beginPos", page.getBeginPos());
        param.put("pageSize", page.getPageSize());
        List<Shop> shopList = restShopClient.getShopListByMap(param);
        for(Shop s:shopList) {
            String fIds =s.getProduct();
            List<String> list = new ArrayList<>();
            for(String a:fIds.split(",")){
                list.add(a);
            }
            Map<String, Object> map = new HashMap<String, Object>();
            param.put("ids", list);
            param.put("parentId", null);
            List<FaultType> faultTypeList = faultTypeClient.getFaultTypeEntityByParentId(param);
            List<String> typeName = new ArrayList<>();
            for(FaultType a:faultTypeList){
                typeName.add(a.getFaultName());
            }
            s.setTypeName(typeName);
        }
        page.setRows(shopList);
        return page;
    }

}
